//
//  WebViewVC.m
//  ShengHuoJia
//
//  Created by xiebin on 2017/12/27.
//  Copyright © 2017年 mingthink. All rights reserved.
//

#import "WebViewVC.h"
#import <JavaScriptCore/JavaScriptCore.h>
#import "JSObjectModel.h"
#import "LoginVC.h"
@interface WebViewVC ()<UIWebViewDelegate,JavaScriptObjectiveCDelegate>
{
    UIWebView *webv_main;
    JSObjectModel *model;
}
@property (nonatomic, strong) JSContext *jsContext;
@end

@implementation WebViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.backBtn.hidden=YES;
    [self initSubViews];
}
-(void)initSubViews{
    webv_main = [[UIWebView alloc]initWithFrame:CGRectMake(0, NAVBAR_HEIGH, DEVW, DEVH-NAVBAR_HEIGH)];
    webv_main.delegate = self;
    webv_main.backgroundColor = [UIColor whiteColor];
    webv_main.opaque = false;//隐藏底部的黑条
    //webv_mainWeb.scrollView.scrollEnabled = false;
    [self.view addSubview:webv_main];
    
    if (self.urlStr && self.urlStr.length > 0){
        NSURL *url = [NSURL URLWithString:self.urlStr];
        DeLog(@"url === %@",url);
        [webv_main loadRequest:[NSURLRequest requestWithURL:url]];
//        NSString *path=[[NSBundle mainBundle] pathForResource:@"abc1" ofType:@"html"];
//        [webv_main loadHTMLString:[NSString stringWithContentsOfURL:[NSURL fileURLWithPath:path] encoding:NSUTF8StringEncoding error:nil] baseURL:nil];
    }
}
#pragma mark -- webViewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
     [self showWaitWithTitle:@"正在加载..."];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self stopWait];
     // self.title=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // 通过模型调用方法，这种方式更好些。
    model  = [[JSObjectModel alloc] init];
    context[@"JsBridge"] = model;
    model.jsContext=context;
    model.delegate = self;
    
    context.exceptionHandler = ^(JSContext *context, JSValue *exceptionValue) {
        context.exception = exceptionValue;
        NSLog(@"异常信息：%@", exceptionValue);
    };
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
        [self stopWait];
}
#pragma mark--------JS方法
-(void)skipToLogin{
    LoginVC *vc = LoginVC.new;
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)skipToShopCar{
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
